﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAnWeb.BUS;
using DoAnWeb.DAO;
using DoAnWeb.DTO;

namespace DoAnWeb.Admin
{
    public partial class Product : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentContext.IsLogged == true)
                {
                    ShowProduct();
                    btnXoa.Visible = false;
                    btnCapNhat.Visible = false;
                }
                else
                    Response.Redirect("~/Default.aspx");
            }
        }
        public void LoadLoaiSanPham()
        {
            LoaiSanPhamDTO DTO = new LoaiSanPhamDTO();
            LoaiSanPhamBUS BUS = new LoaiSanPhamBUS();
            dlLoaiSanPham.DataSource = BUS.GetList();
            dlLoaiSanPham.DataTextField = "TenLoaiSanPham";
            dlLoaiSanPham.DataValueField = "MaLoaiSanPham";
            dlLoaiSanPham.DataBind();
        }

        public void ShowProduct()
        {
            TaiKhoanDAO DAO = new TaiKhoanDAO();
            if (DAO.GetKind(CurrentContext.UserName) != "LTK1")
            {
                Response.Redirect("~/Default.aspx");
            }
            TinKhuyenMaiBUS bus = new TinKhuyenMaiBUS();
            gvListTinKhuyenMai.DataSource = bus.GetList_ChuaXoa();
            gvListTinKhuyenMai.DataBind(); 
            LoadLoaiSanPham();
        }
        protected void gvListTinKhuyenMai_RowEditing(object sender, GridViewEditEventArgs e)
        { 
            gvListTinKhuyenMai.EditIndex = e.NewEditIndex;
            gvListTinKhuyenMai.SelectedIndex = e.NewEditIndex;
            ShowProduct();
        }

        protected void gvListTinKhuyenMai_RowDeleting(object sender, GridViewDeleteEventArgs e)
        { 
            int MaTinKhuyenMai = Convert.ToInt32(gvListTinKhuyenMai.DataKeys[e.RowIndex].Value);
            TinKhuyenMaiBUS bus = new TinKhuyenMaiBUS();
            bus.CheckDelete(MaTinKhuyenMai);
            ShowProduct();
        }

        protected void gvListTinKhuyenMai_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvListTinKhuyenMai.EditIndex = -1;
            ShowProduct();
        }

        protected void gvListTinKhuyenMai_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListTinKhuyenMai.PageIndex = e.NewPageIndex;
            gvListTinKhuyenMai.SelectedIndex = -1;
            ShowProduct();
            gvListTinKhuyenMai.DataBind();
        }

        protected void gvListTinKhuyenMai_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            TinKhuyenMaiDTO DTO = new TinKhuyenMaiDTO();
            TinKhuyenMaiBUS BUS = new TinKhuyenMaiBUS();
            int MaTinKhuyenMai = Convert.ToInt32(gvListTinKhuyenMai.DataKeys[e.RowIndex].Value);

            gvListTinKhuyenMai.SelectedIndex = e.RowIndex;
            DTO = BUS.GetFromId(MaTinKhuyenMai);
            DTO.TieuDe = (gvListTinKhuyenMai.Rows[e.RowIndex].FindControl("txtTieuDeEdit") as TextBox).Text;
            DTO.GiaGoc = int.Parse((gvListTinKhuyenMai.Rows[e.RowIndex].FindControl("txtGiaGocEdit") as TextBox).Text);
            DTO.PhanTramKhuyenMai = int.Parse((gvListTinKhuyenMai.Rows[e.RowIndex].FindControl("txtPhanTramKMEdit") as TextBox).Text);
            BUS.Update(DTO);
            gvListTinKhuyenMai.EditIndex = -1;
            ShowProduct();
        }

        protected void gvListTinKhuyenMai_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "btnDuyet")
            {
                
                int index = int.Parse(e.CommandArgument.ToString());
                int MaTinKhuyenMai = int.Parse((gvListTinKhuyenMai.Rows[index].Cells[0].Controls[1] as Label).Text);
                TinKhuyenMaiBUS bus = new TinKhuyenMaiBUS();
                bus.Duyet(MaTinKhuyenMai);
                gvListTinKhuyenMai.EditIndex = -1;
                
                ShowProduct();
            }
           

        }

        protected void gvListTinKhuyenMai_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void gvListTinKhuyenMai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gvListTinKhuyenMai.SelectedIndex > -1)
            {
                int index = gvListTinKhuyenMai.SelectedIndex;
                int MaTinKhuyenMai = int.Parse(gvListTinKhuyenMai.DataKeys[index].Value.ToString());
                TinKhuyenMaiBUS bus =new TinKhuyenMaiBUS();
                TinKhuyenMaiDTO dto = new TinKhuyenMaiDTO();
                dto = bus.GetFromId(MaTinKhuyenMai);
                txtTieuDe.Text = dto.TieuDe;
                txtPhanTram.Text = Convert.ToString(dto.PhanTramKhuyenMai);
                txtGiaGoc.Text = Convert.ToString(dto.GiaGoc);
                txtDienThoai.Text = dto.DienThoai;
                txtDiaChi.Text = dto.DiaChi;
                ckNoiDung.Text = dto.MoTa;
                dlLoaiSanPham.SelectedValue = dto.MaLoaiSanPham.ToString();
                btnXoa.Visible = true;
                btnCapNhat.Visible = true;

                lbThongBao.Visible = false;
            }
        }

        

        protected void gvListTinKhuyenMai_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    LinkButton b = e.Row.Cells[8].Controls[0] as LinkButton;
            //    if (b != null)
            //    {
            //        b.OnClientClick = "return confirm('Xóa?')";
            //    }
            //}
        }
         
        protected void btnXoa_Click(object sender, EventArgs e)
        {
            int index = gvListTinKhuyenMai.SelectedIndex;
            int MaTinKhuyenMai = int.Parse(gvListTinKhuyenMai.DataKeys[index].Value.ToString());
            TinKhuyenMaiBUS bus = new TinKhuyenMaiBUS();
            TinKhuyenMaiDTO dto = new TinKhuyenMaiDTO();

            bus.CheckDelete(MaTinKhuyenMai);
            txtTieuDe.Text = "";
            txtPhanTram.Text = "";
            txtGiaGoc.Text = "";
            txtDienThoai.Text = "";
            txtDiaChi.Text = "";
            ckNoiDung.Text = "";
            btnXoa.Visible = false;
            btnCapNhat.Visible = false;
            dlLoaiSanPham.SelectedIndex =0;
            gvListTinKhuyenMai.SelectedIndex = -1;
            ShowProduct(); 
            lbThongBao.Text = "Đã Xóa";
            lbThongBao.Visible = true;
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            int index = gvListTinKhuyenMai.SelectedIndex;
            int MaTinKhuyenMai = int.Parse(gvListTinKhuyenMai.DataKeys[index].Value.ToString());
            TinKhuyenMaiBUS bus = new TinKhuyenMaiBUS();
            TinKhuyenMaiDTO dto = new TinKhuyenMaiDTO();
            dto = bus.GetFromId(MaTinKhuyenMai);
            dto.TieuDe = txtTieuDe.Text;
            dto.MoTa = ckNoiDung.Text;
            dto.DiaChi = txtDiaChi.Text;
            dto.DienThoai = txtDienThoai.Text;
            dto.GiaGoc = int.Parse(txtGiaGoc.Text);
            dto.GiaBan = int.Parse(txtGiaGoc.Text) - (int.Parse(txtGiaGoc.Text) * int.Parse(txtPhanTram.Text) / 100);
            dto.PhanTramKhuyenMai = int.Parse(txtPhanTram.Text);
            dto.MaLoaiSanPham = int.Parse(dlLoaiSanPham.SelectedValue.ToString());
            
            bus.Update(dto);

            txtTieuDe.Text = "";
            txtPhanTram.Text = "";
            txtGiaGoc.Text = "";
            txtDienThoai.Text = "";
            txtDiaChi.Text = "";
            ckNoiDung.Text = "";
            btnXoa.Visible = false;
            btnCapNhat.Visible = false;
            dlLoaiSanPham.SelectedIndex = 0;
            gvListTinKhuyenMai.SelectedIndex = -1;
            ShowProduct();
            lbThongBao.Text = "Đã cập nhật";
            lbThongBao.Visible = true;
        }

      
       
    }
}